package com.tnstudio.shaketoopenapps;
import android.app.Service;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.IBinder;
import android.util.Log;


public class MyService extends Service implements AccelerometerListener {

	
	
	@Override
	public IBinder onBind(Intent intent) {

		Log.d("MyService", "onBind");
		return null;
	}

	@Override
	public void onCreate() {

		Log.d("MyService", "onCreate");

		super.onCreate();
	}

	@SuppressWarnings("deprecation")
	@Override
	public void onStart(Intent intent, int startId) {
		Log.d("MyService", "onStart");
		SettingData.showToast(getBaseContext(), "Khoi chay service");
		if (AccelerometerManager.isSupported(this)) {

			AccelerometerManager.startListening(this);
		}
		super.onStart(intent, startId);
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {

		Log.d("MyService", "onStartCommand");
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public void onDestroy() {

		Log.d("MyService", "onDestroy");
		// Check device supported Accelerometer senssor or not
		if (AccelerometerManager.isListening()) {

			AccelerometerManager.stopListening();
			SettingData.showToast(getBaseContext(), "Thiet bi khong ho tro");
		}
		super.onDestroy();
	}

	@Override
	public boolean onUnbind(Intent intent) {

		Log.d("MyService", "onUnbind");
		return super.onUnbind(intent);
	}

	@Override
	public void onAccelerationChanged(float x, float y, float z) {

	}

	@Override
	public void onShake(float force) {
		SettingData.showToast(getBaseContext(), "Lac may");
		PackageManager packageManager = null;
		packageManager =  getPackageManager();
		MySharedPreferences share = new MySharedPreferences(getBaseContext());
		share.restoringPreferences(null, null, null, null);
		String pack = share.getChoosePack();
		Intent intent = packageManager.getLaunchIntentForPackage(pack);
		if (null != intent) {
			startActivity(intent);
		}
		else
		{
			SettingData.showToast(getBaseContext(), "Erros");
		}

	}
	

}